package com.me.bbs.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.me.bbs.anno.RequireLogin;
import com.me.bbs.common.Resp;
import com.me.bbs.common.SessionUtils;
import com.me.bbs.dto.PostCollectionDTO;
import com.me.bbs.dto.ViewRecordDTO;
import com.me.bbs.entity.PostCollection;
import com.me.bbs.entity.ViewRecord;
import com.me.bbs.service.IViewRecordService;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lm
 * @since 2023-01-09
 */
@RestController
@RequestMapping("/viewRecord")
public class ViewRecordController {
    @Resource
    IViewRecordService viewRecordService;

    @PostMapping("/page")
    @RequireLogin
    public Resp<?> page(@RequestParam("current") Long current,
                        @RequestParam("size") Long size
    ) {
        Page<ViewRecord> page = new Page<>(current, size);
        LambdaQueryWrapper<ViewRecord> wrapper = new LambdaQueryWrapper<>();
        Long userId = (Long) SessionUtils.getAttribute("user");
        wrapper.eq(userId != null, ViewRecord::getUser, userId)
                .orderByDesc(ViewRecord::getViewTime);
        Page<ViewRecordDTO> viewRecordDTOPage = viewRecordService.getViewRecordList(page, wrapper);
        return Resp.success(viewRecordDTOPage, "成功");
    }

    @DeleteMapping("/{id}")
    @RequireLogin
    public Resp<?> deleteRecord(@PathVariable Long id) {
        boolean remove = viewRecordService.lambdaUpdate().eq(ViewRecord::getRecordId, id)
                .remove();
        if (remove)
            return Resp.success();
        return Resp.error();
    }
}
